﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QuanLyKhachSan.BusinessLogic;
using QuanLyKhachSan.BusinessObject;
using QuanLyKhachSan.DataAccess;
namespace QuanLyKhachSan
{
    public partial class frmKhachHang : Form
    {
        private KhachHangBO khBO = new KhachHangBO();
        private KhachHangBL khBL = new KhachHangBL();
        string makh = null;
        public frmKhachHang()
        {

            InitializeComponent();
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            colGioiTinh.DataSource = khBL.LayDSGioiTinh();
            colGioiTinh.DisplayMember = "NameGT";
            colGioiTinh.ValueMember = "GioiTinh";
            dgvwKhachHang.DataSource = khBL.LayDSKhachHang();

        }

        private void dgvwKhachHang_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int dong = e.RowIndex;
            khBO.STT = int.Parse(dgvwKhachHang.Rows[dong].Cells["STT"].Value.ToString());
            khBO.MaKhachHang = dgvwKhachHang.Rows[dong].Cells["MaKhachHang"].Value.ToString();
            khBO.TenKhachHang = dgvwKhachHang.Rows[dong].Cells["TenKhachHang"].Value.ToString();
            khBO.SoDienThoai = dgvwKhachHang.Rows[dong].Cells["SoDienThoai"].Value.ToString();
            khBO.SoChungMinhThu = dgvwKhachHang.Rows[dong].Cells["SoChungMinhThu"].Value.ToString();
            khBO.NgaySinh = Convert.ToDateTime(dgvwKhachHang.Rows[dong].Cells["NgaySinh"].Value.ToString());
            khBO.DiaChi = dgvwKhachHang.Rows[dong].Cells["DiaChi"].Value.ToString();
            if (dgvwKhachHang.Rows[dong].Cells["colGioiTinh"].Value.ToString().Equals("False"))
            {
                khBO.GioiTinh = 0;
            }
            if (dgvwKhachHang.Rows[dong].Cells["colGioiTinh"].Value.ToString().Equals("True"))
            {
                khBO.GioiTinh = 1;
            }
        }

        private void LayDuLieu(KhachHangBO KH)
        {
            this.makh = KH.MaKhachHang;
            if (!makh.Equals(""))
            {
                dgvwKhachHang.DataSource = khBL.LayDSKhachHang();
            }
        }




        private void btnThem_Click_1(object sender, EventArgs e)
        {
            frmAddKhachHang faKH = new frmAddKhachHang();
            faKH.DuLieu = new frmAddKhachHang.DuLieuTruyenVe(LayDuLieu);
            faKH.NextID();
            faKH.Show();
        }

        private void btnSua_Click_1(object sender, EventArgs e)
        {
            frmAddKhachHang faKH = new frmAddKhachHang();
            faKH.DuLieu = new frmAddKhachHang.DuLieuTruyenVe(LayDuLieu);
            faKH.Load_Edit(khBO);
            faKH.Show();
        }

        private void btnXoa_Click_1(object sender, EventArgs e)
        {
            if (khBO.MaKhachHang == "")
                MessageBox.Show("Chọn mã nhân viên", "Chương trình thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa hay không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    khBL.Delete(khBO.MaKhachHang);
                    frmKhachHang_Load(sender, e);
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            bool kq = (MessageBox.Show("Bạn có chắc chắn muốn thoát không ? ", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes);
            if (kq == true)
            {
                this.Close();
            }
        }

        private void txtTimKiem_KeyUp_1(object sender, KeyEventArgs e)
        {
            dgvwKhachHang.DataSource = khBL.TimKiemKhachHang(txtTimKiem.Text);
        }
    }
}
